package org.aplombh.java.interview.newcoder;

import java.util.*;

public class HJ43_迷宫问题 {

    private static final Stack<Pair> stack = new Stack<Pair>();
    private static final ArrayList<Pair> path = new ArrayList<Pair>();
    private static final int[][] ints = new int[10][10];
    private static final int[][] visit = new int[10][10];
    private static final int[] dx = {-1, 0, 1, 0};
    private static final int[] dy = {0, 1, 0, -1};
    private static boolean flag = false;
    private static int n;
    private static int m;

    private static class Pair {
        public int l;
        public int r;
        public Pair(int l, int r) {
            this.l = l;
            this.r = r;
        }
        public Pair() {
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            n = in.nextInt();
            m = in.nextInt();

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    ints[i][j] = in.nextInt();
                }
            }

            dfs(0, 0);
        }
    }

    private static void dfs(int x, int y) {
        // 无法行走
        if (x < 0 || x >= n || y < 0 || y >= m || visit[x][y] == 1 || ints[x][y] == 1 || flag)
            return;

        stack.push(new Pair(x, y));
        path.add(new Pair(x, y));
        visit[x][y] = 1;


        if (x == n - 1 && y == m - 1) {
            flag = true;
            for (Pair pair : path) {
                System.out.println("(" + pair.l + "," + pair.r + ")");
            }
            return;
        }

        for (int i = 0; i < 4; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];
            dfs(tx, ty);
        }

        visit[x][y] = 0;
        path.remove(path.size() - 1);
        stack.pop();
    }
}
